package com.itchen.config;

import org.mybatis.spring.SqlSessionFactoryBean;
import org.mybatis.spring.mapper.MapperScannerConfigurer;
import org.springframework.context.annotation.Bean;

import javax.sql.DataSource;

/**
 * @author げっしょく
 * @date 2021/9/15
 */

public class SpringMabtisConfig {
    @Bean
    public SqlSessionFactoryBean sqlSessionFactoryBean(DataSource dataSource) {
        SqlSessionFactoryBean sql = new SqlSessionFactoryBean();
        //com.itheima.domain  是对应的POJO类，作用时起别名
        sql.setTypeAliasesPackage("com.itchen.domain");
        //将访问的数据库等消息传入里面
        sql.setDataSource(dataSource);

        return sql;
    }

    @Bean
    //扫描dao层接口所在的包，根据接口和对应的SQL产生接口的实现类对象
    public MapperScannerConfigurer mapperScannerConfigurer() {
        MapperScannerConfigurer msc = new MapperScannerConfigurer();
        //Mapper代理，作用：创建dao层的实现类对象
        msc.setBasePackage("com.itchen.dao");
        return msc;
    }

}
